package com.meiyou.controller;

import com.meiyou.dto.AddArticleDto;
import com.meiyou.dto.ArticlePageQueryDto;
import com.meiyou.dto.StarPageDto;
import com.meiyou.dto.UpdateArticleDto;
import com.meiyou.result.PageResult;
import com.meiyou.result.Result;
import com.meiyou.service.ArticleService;
import com.meiyou.vo.ArticleDetailVo;
import com.meiyou.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: meiyou
 * @BelongsPackage: com.meiyou.controller
 * @Author: 尹晓安
 * @Date: 2023/11/25 18:03
 * @Description: todo
 */
@RestController
@RequestMapping("/api/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     *
     * @param addArticleDto
     * @return
     */
    @PostMapping
    public Result add(@RequestBody AddArticleDto addArticleDto) {

        articleService.add(addArticleDto);

        return Result.success("添加成功！");
    }

    /**
     * 修改文章
     *
     * @param updateArticleDto
     * @return
     */
    @PutMapping
    public Result updateArticle(@RequestBody UpdateArticleDto updateArticleDto) {

        articleService.updateArticle(updateArticleDto);

        return Result.success("修改成功！");
    }

    /**
     * 根据用户获取文章,分页
     *
     * @param articlePageQueryDto
     * @return
     */
    @GetMapping("/pageByUser")
    public Result<PageResult> getArticleByUserId(ArticlePageQueryDto articlePageQueryDto) {

        PageResult pageResult = articleService.getArticleByUserId(articlePageQueryDto);

        return Result.success(pageResult);
    }

    /**
     * 分页获取文章
     *
     * @param articlePageQueryDto
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> getArticle(ArticlePageQueryDto articlePageQueryDto) {

        PageResult pageResult = articleService.getArticle(articlePageQueryDto);

        return Result.success(pageResult);
    }

    /**
     * 分页获取用户点赞文章
     *
     * @param articlePageQueryDto
     * @return
     */
    @GetMapping("/star")
    public Result<PageResult> getStarByUser(ArticlePageQueryDto articlePageQueryDto) {

        PageResult pageResult = articleService.getStarArticle(articlePageQueryDto);

        return Result.success(pageResult);
    }

    /**
     * 分页获取用户收藏文章
     *
     * @param articlePageQueryDto
     * @return
     */
    @GetMapping("/collect")
    public Result<PageResult> getCollectByUser(ArticlePageQueryDto articlePageQueryDto) {

        PageResult pageResult = articleService.getCollectArticle(articlePageQueryDto);

        return Result.success(pageResult);
    }

    /**
     * 删除文章
     *
     * @param id 文章id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable Long id) {

        articleService.deleteArticle(id);

        return Result.success("删除成功！");
    }

    /**
     * 审核文章
     *
     * @param id     文章id
     * @param status 文章状态，1审核通过、0审核不通过、2审核中
     * @return
     */
    @PutMapping("/{id}/{status}")
    public Result examineArticle(@PathVariable Long id, @PathVariable Integer status) {

        articleService.examineArticle(id, status);

        return Result.success("操作成功！");
    }

    /**
     * 获取文章详情
     *
     * @param id 文章id
     * @return
     */
    @GetMapping("/{id}")
    public Result<ArticleDetailVo> getArticleDetail(@PathVariable Long id) {

        ArticleDetailVo articleDetailVo = articleService.getArticleDetail(id);

        return Result.success(articleDetailVo);
    }

    /**
     * 管理员获取文章（进行审核）根据文章状态获取
     *
     * @param status 文章状态，1审核通过、0审核不通过、2审核中
     * @return
     */
    @GetMapping("/admin/{status}")
    public Result<PageResult> getAdminArticle(@PathVariable Integer status, ArticlePageQueryDto articlePageQueryDto) {

        PageResult pageResult = articleService.getAdminArticle(status, articlePageQueryDto);

        return Result.success(pageResult);
    }

}
